﻿




$.MGJ.Recall = [];

(function () {
    var _this = $.MGJ.Recall;
    $.extend($.MGJ.Recall, {



        conditionModel: {
            page: 1,
            pageSize: 10,
            searchKey: null,//查询条件
            action: 0

        },

        Ajax_GetCodeRecallPageList: function (pageIndex, action) {
         
            //var searchKey = $("#SearchKey").val();
            $.L.ajaxHandle({
                url: ACTION_URL("GetCodeRecallPageList", "CodeManage"),
                data: {
                    //searchKey: searchKey,
                    pageIndex: pageIndex,
                    pageSize: 10,
                    action: 2
                },
                success: function (response) {
                    $("#PageList").L_bindingDataList(response);
                    $(".StartCode").each(function () {

                        if ($(this).text() == 0) {
                            $(this).html("")
                        }

                    })
                    $(".EndCode").each(function () {

                        if ($(this).text() == 0) {
                            $(this).html("")
                        }

                    })
                    //$(".RelateCodeNums").each(function () {

                    //    if ($(this).text() == 0) {
                    //        $(this).html("整批")
                    //    }

                    //})
                }
            });
        },

        Ajax_GetCodeRecallDetailPageList:function(pageIndex){
        
            $.L.ajaxHandle({
                url: ACTION_URL("GetCodeRecallDetailPageList", "CodeManage"),
                data: {
                    pageIndex: pageIndex,
                    pageSize: 10,

                },
                success: function (response) {
                    $("#PageList").L_bindingDataList(response);
                }
            });
        
        
        
        },

        Dialog_EditObj:function(obj){
            var distributorrecalllogId = $(obj).attr("PID");
            $.L.ajaxHandle({
                url: ACTION_URL("UpdateDistributorRecallLogStatus", "CodeManage"),
                data: {
                    Id: distributorrecalllogId
                },
                success: function (response) {
                    if (response >= 0) {
                        _this.Ajax_GetCodeRecallDetailPageList(1);
                        $.L.msgSuccess("操作成功");
                    } else {
                        $.L.msgSuccess("网络异常");
                    }
            
                }
            });



        },






        AllotTypeName: function (val) {
            if (val == 0) {
                return "单码";
            }
            if (val == 1) {
                return "整批";
            }
            ////按整版分配 计算总数
            //if (allotType == 1) {
            //    _this.ReturnSelectCount(2);
            //}
        },

        Status:function(val){
            if (val == 0) {
                return "待召回";
            }
            if (val == 1) {
                return "已确定";
            }
        
        
        },


        //计算已选数量
        ReturnSelectCount: function (type) {

            if (type == 1) {
                //var _startCode = Number($("#StartCode").val());
                //var _endCode = Number($("#EndCode").val());
                //if (_startCode <= 0) {
                //    $("#StartCode").val(0);
                //}
                //if (_endCode <= 0) {
                //    $("#EndCode").val(0);
                //    return;
                //}
                //if (_endCode >= _startCode) {
                //    $("#RelateCodeNums").val((_endCode - _startCode));
                //} else {
                //    $("#RelateCodeNums").val(0);
                //}
            } else {
                //计算整版数量
                var _batchNum = $("#CodeBatch").find("option:selected").text();

                if (_batchNum) {
                    $.L.ajaxHandle({
                        url: ACTION_URL("GetWholeBatchCount", "CodeManage"),
                        type: "GET",
                        data: { batchNo: _batchNum },
                        success: function (r) {
                            $("#RelateCodeNums").val(r);
                        }
                    })
                }
            }
        },



        Dialog_RecallAddObj: null,
        Dialog_RecallAdd: function (obj) {

            this.Dialog_RecallAddObj = $.L.dialog({
                url: ACTION_URL("RecallAdd", "CodeManage"),
                title: "预警召回",
                width: 800,
                height: "auto",
                buttons: {
                    "button_save": {
                        type: 'primary',
                        text: '下一步',
                        click: function () {
                            var bootstrapValidator = $("#AEForm").data('bootstrapValidator').validate();//触发全部验证
                            if (bootstrapValidator.isValid()) {
                                //_this.Ajax_RecallSave();
                                _this.Ajax_GetReList();
                            }
                            else {
                                _this.Ajax_GetReList();
                                //$.L.msgError("请检查数据格式！");
                            }
                        }
                    },
                    "button_cancel": {
                        type: 'default',
                        text: '取消',
                        click: 'MODALMISS'
                    }
                }
            });
        },


        Dialog_RecallListObj: null,
        Ajax_GetReList: function () {
       
            var data = $("#AEForm").serialize();
            this.Dialog_RecallListObj= $.L.dialog({
                url: ACTION_URL("ReList", "CodeManage")+"?"+data,
                //url: ACTION_URL("ReList", "CodeManage"),
                title: "召回清单",
                width: 800,
                height: 'auto',
                buttons: {
                    "button_save": {
                        type: 'primary',
                        text: '召回',
                        click: function () {
                                _this.Ajax_RecallSave();
                        }
                    },
                    "button_cancel": {
                        type: 'default',
                        text: '取消',
                        click: 'MODALMISS'
                    }
                }
            });

        },


        Ajax_RecallSave:function(){
        
            var data = $("#AEForm").serializeObject();
     
            var DistributorList = $("#mainContent input[name='DistributorID']");

            var list = [];
            for (var i = 0; i < DistributorList.length; i++) {

                item = DistributorList[i];
                list[i] = item.value;

            }
            //data.DistributorList = list;

            var CodeReCallModel = function () {
                this.Model = data;
                this.DistributorList = list;
            }

            var CodeReCall = new CodeReCallModel();


            $.ajax({
                url: "/CodeManage/RecallSave",
                type: "POST",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                data: "{\"CodeReCallModel\":" + JSON.stringify(CodeReCall) + "}",
                success: function (result) {
              
                    if (result > 0) {
                        $.L.msgSuccess("操作成功");
                                    _this.Dialog_RecallAddObj.close();
                                    _this.Dialog_RecallListObj.close();

                                    //刷新列表
                                    _this.Ajax_GetCodeRecallPageList(1,2)
    
                    } else {
                        $.L.msgSuccess("操作失败，请检查网络是否异常");
                    }
                }
            });






            //$.L.ajaxHandle({
            //    url: ACTION_URL("RecallSave", "CodeManage")+"?test=1",
            //    data: data,
            //    type: 'POST',
            //    success: function (result) {
            //        if (result == true) {
            //            //_this.Ajax_GetRecallPageList(1, 0);
            //            _this.Dialog_RecallAddObj.close();
            //            _this.Dialog_RecallListObj.close();

            //            //刷新列表
            //            _this.Ajax_GetCodeRecallPageList(1,2)
            //            $.L.msgSuccess("操作成功！");
            //        }
            //        else {
            //            $.L.msgWarning("请检查网络");
            //        }
            //    }
            //});
        
        },

      

        //Ajax_RecallSave: function () {

        //    var data = $("#RecallForm").serialize();

        //    $.L.ajaxHandle({
        //        url: ACTION_URL("IsCanInsertAndBinding", "CodeManage"),
        //        data: data,
        //        type: 'POST',
        //        success: function (result) {

        //            var isInsert = true;

        //            if (result == false) {
        //                var _confirm = confirm("当前码已经绑定冻结，是否重复冻结?")

        //                if (_confirm) {

        //                    isInsert = true

        //                } else {
        //                    isInsert = false
        //                    _this.Ajax_GetRecallPageList(1, 0);
        //                    _this.Dialog_RecallAddObj.close();
        //                    $.L.msgSuccess("已取消！");
        //                    return;
        //                }
        //            }
        //            if (isInsert == true) {
        //                $.L.ajaxHandle({
        //                    url: ACTION_URL("RecallSave", "CodeManage"),
        //                    data: data,
        //                    type: 'POST',
        //                    success: function (result) {
        //                        if (result == true) {
        //                            _this.Ajax_GetRecallPageList(1, 0);
        //                            _this.Dialog_RecallAddObj.close();
        //                            $.L.msgSuccess("操作成功！");
        //                        }
        //                        else {
        //                            $.L.msgWarning("请检查网络");
        //                        }
        //                    }
        //                });
        //            }

        //        }
        //    });

        //},



        //Ajax_CodeDisplaySave: function (obj) {

        //    var data = $("#RecallForm").serialize();
        //    var RelateCodeNums = $("#RelateCodeNums").val();

        //    $.L.ajaxHandle({
        //        url: ACTION_URL("RecallSave", "CodeManage") + "?RelateCodeNums=" + RelateCodeNums,
        //        data: data,
        //        type: 'POST',
        //        success: function (result) {
        //            if (result == true) {
        //                _this.Ajax_GetRecallPageList(1, 1);
        //                _this.Dialog_CodeDisableAddObj.close();
        //                $.L.msgSuccess("操作成功！");
        //            }
        //            else {
        //                $.L.msgWarning("请检查该批次码是否被冻结或码数据是否存在！");
        //            }
        //        }
        //    });
        //},


        RelateTypeChenge: function (_this) {
            var index = $("input[name = 'RelateType']").index($(_this))
            index === 0 ? $("#SectionNum").show("fast") && $("#SectionNum2").hide("fast") : $("#SectionNum2").show("fast") && $("#SectionNum").hide("fast")
        }

    });
})(jQuery);

